package DBUtility;

import java.util.Hashtable;
import java.util.Iterator;

import DBUtility.SearchInfo;
import DBUtility.SqlOperator;

public class SearchCondition
{
    private Hashtable<String, SearchInfo> conditionTable = new Hashtable<String, SearchInfo>();
    public Hashtable<String, SearchInfo> ConditionTable()
    {
         return conditionTable; 
    }

    public SearchCondition AddCondition(String fieldName, Object fieldValue, SqlOperator sqlOperator)
    {
        conditionTable.put(fieldName, new SearchInfo(fieldName, fieldValue, sqlOperator));
        return this;
    }

    public SearchCondition AddCondition(String fielName, Object fieldValue, SqlOperator sqlOperator, boolean excludeIfEmpty)
    {
        this.conditionTable.put(fielName, new SearchInfo(fielName, fieldValue, sqlOperator, excludeIfEmpty));
        return this;
    }

    public String BuildConditionSql()
    {
        String sql = " WHERE (1=1) ";       // Append "(1=1)" to in order to connect with " AND..." condition
        SearchInfo searchInfo = null;
        Iterator<String> iter = conditionTable.keySet().iterator();

        StringBuilder sb = new StringBuilder();
        while (iter.hasNext())
        {
            Object key = iter.next();
            Object val = conditionTable.get(key);
            searchInfo = (SearchInfo)val;
            
            if (searchInfo.isExcludeIfEmpty() && (String)searchInfo.getFieldValue() == "")
            {
                continue;
            }
            
            if (searchInfo.getSqlOperator() == SqlOperator.Like)
            {
                sb.append(" AND " + searchInfo.getFieldName());
                sb.append(" " + this.ConvertSqlOperator(searchInfo.getSqlOperator()));
                sb.append(" %" + searchInfo.getFieldValue() + "%");
            }
            else
            {
                sb.append(" AND " + searchInfo.getFieldName());
                sb.append(" " + this.ConvertSqlOperator(searchInfo.getSqlOperator()));
                sb.append(" " + searchInfo.getFieldValue());
            }
        }
        
        sql += sb.toString();

        return sql;
    }
    
    private String ConvertSqlOperator(SqlOperator sqlOperator)
    {
        String stringOperator = " = ";
        switch (sqlOperator)
        {
            case Equal:
                stringOperator = " = ";
                break;
            case LessThan:
                stringOperator = " < ";
                break;
            case LessThanOrEqual:
                stringOperator = " <= ";
                break;
            case Like:
                stringOperator = " Like ";
                break;
            case MoreThan:
                stringOperator = " > ";
                break;
            case MoreThanOrEqual:
                stringOperator = " >= ";
                break;
            case NotEqual:
                stringOperator = " <> ";
                break;
            default:
                break;
        }

        return stringOperator;
    }

//    private DbType GetFieldDbType(object fieldValue)
//    {
//        DbType type = DbType.String;
//
//        switch (fieldValue.GetType().ToString())
//        {
//            case "System.Int16":
//                type = DbType.Int16;
//                break;
//            case "System.UInt16":
//                type = DbType.UInt16;
//                break;
//            case "System.Single":
//                type = DbType.Single;
//                break;
//            case "System.UInt32":
//                type = DbType.UInt32;
//                break;
//            case "System.Int32":
//                type = DbType.Int32;
//                break;
//            case "System.UInt64":
//                type = DbType.UInt64;
//                break;
//            case "System.Int64":
//                type = DbType.Int64;
//                break;
//            case "System.String":
//                type = DbType.String;
//                break;
//            case "System.Double":
//                type = DbType.Double;
//                break;
//            case "System.Decimal":
//                type = DbType.Decimal;
//                break;
//            case "System.Byte":
//                type = DbType.Byte;
//                break;
//            case "System.Boolean":
//                type = DbType.Boolean;
//                break;
//            case "System.DateTime":
//                type = DbType.DateTime;
//                break;
//            case "System.Guid":
//                type = DbType.Guid;
//                break;
//            default:
//                break;
//        }
//        return type;
//    }
}